﻿using InventoryManagementSystem.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InventoryManagementSystem.DataModel
{
    public class Order : BindableBase
    {
        public static Order Create()
        {
            var order = new Order();
            order.product = new Product();
            return order;
        }

        public Order()
        {
        }

        private Guid id = Guid.Empty;
        public Guid Id
        {
            get
            {
                return id;
            }
            set
            {
                SetProperty(ref id, value);
            }
        }

        private DateTime time;
        public DateTime Time
        {
            get
            {
                return time;
            }
            set
            {
                SetProperty(ref time, value);
            }
        }

        private Product product;
        public Product Product
        {
            get
            {
                return product;
            }
            set
            {
                SetProperty(ref product, value);
            }
        }

        private decimal unitPrice;
        public decimal UnitPrice
        {
            get
            {
                return unitPrice;
            }
            set
            {
                SetProperty(ref unitPrice, value);
                SetProperty(ref totalPrice, value * count, "TotalPrice");
            }
        }

        private decimal count;
        public decimal Count
        {
            get
            {
                return count;
            }
            set
            {
                SetProperty(ref count, value);
                SetProperty(ref totalPrice, unitPrice * value, "TotalPrice");
            }
        }

        private decimal totalPrice;
        public decimal TotalPrice
        {
            get
            {
                return totalPrice;
            }
            set
            {
                SetProperty(ref totalPrice, value);
                if (count != 0)
                {
                    SetProperty(ref unitPrice, value / count, "ProductUnitPrice");
                }
            }
        }

        public void Reset()
        {
            Id = Guid.Empty;
            Product = new Product();
            UnitPrice = 0;
            Count = 0;
        }

        public Order Clone()
        {
            var order = new Order();
            order.Id = Id;
            order.Product = Product.Clone();
            order.UnitPrice = UnitPrice;
            order.Count = Count;
            // Total price will be calculated
            return order;
        }

        public bool Validate()
        {
            return UnitPrice > 0 &&
                Count > 0 &&
                Product != null &&
                Product.Validate();
        }
    }
}
